package com.jbinfo.mongoot.controller;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jbinfo.mongoot.operation.oldReturnVist.OldReturnVist;
import com.jbinfo.mongoot.operation.oldReturnVist.OldReturnVistReposityory;

@RestController
@RequestMapping(value="/visit")
@Api("test_mongo-old_return_vist类Controller")
public class OldReturnVistController {
	@Autowired
	private OldReturnVistReposityory oldReturnVistReposityory;
	 @Autowired
	 private MongoTemplate mongoTemplate;
	/**
	 * 通过post保存老人回访记录
	 * @param oldReturnVist
	 * @return
	 */
	@ApiOperation("保存老人回访记录")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public Map<String, Object> add(@ModelAttribute OldReturnVist oldReturnVist){
		Map<String, Object> map = new HashMap<String, Object>();
		String description="";
		//验证参数是否为空
		if(oldReturnVist.getOldId()==null ||  "".equals(oldReturnVist.getOldId())){
			description+= "老人id不能为空、";
		}
		if(oldReturnVist.getType()==null || "".equals(oldReturnVist.getType())){
			description+= "回访类型不能为空、";
		} 
		if(oldReturnVist.getScore()==null || "".equals(oldReturnVist.getScore())){
			description+= "评分不能为空、";
		}
		if(oldReturnVist.getCreateManagerId()==null || "".equals(oldReturnVist.getCreateManagerId())){
			description+= "创建人id不能为空、";
		}
		if(oldReturnVist.getCreateManagerName()==null || "".equals(oldReturnVist.getCreateManagerName())){
			description+= "创建人姓名不能为空";
		}
		if("".equals(description)){//非空
			Date currentTime = new Date();
		    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(currentTime);
			oldReturnVist.setIsvalide("1");
			oldReturnVist.setCreatTime(dateString);
			oldReturnVistReposityory.save(oldReturnVist);
			map.put("status", 1);
		}else{
			map.put("status", 0);
		}
		map.put("description", description);
		map.put("code", "");
		return map;
	}
	/**
	 * 通过老人id获得老人所有的回访记录
	 * @param old_id
	 * @return
	 */
	@ApiOperation("查询老人所有的回访记录")
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public Map<String, Object> getByOldId(@RequestParam("old_id") String old_id){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", 1);
		map.put("description", "");
		map.put("code", "");
		if(old_id.isEmpty()){
			map.put("status", 0);
			map.put("description", "老人id不能为空");
			return map;
		}else{
			List<OldReturnVist> list = oldReturnVistReposityory.findByOldIdAndIsvalideOrderByCreatTimeDesc(old_id, "1");
			map.put("result", list);
			return map;
		}
	}
	/**
	 * 测试
	 * @return
	 */
	@RequestMapping(value="/test",method = RequestMethod.GET)
	public OldReturnVist test(){
		Query query = new Query(Criteria.where("oldId").is("2"));
		return mongoTemplate.findOne(query, OldReturnVist.class);
	}
}
